package com.xhj.bigfactory.bigFactory.ThreadPool;
import java.util.concurrent.*;
/**
 * @author haijianxia pure_xhj@163.com
 * @Date 2021-09-17
 * @Time 9:57
 */
public class CompletableFutureDemo {
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		ExecutorService executorService = Executors.newCachedThreadPool();
		executorService.submit(new Callable<Object>() {
			@Override
			public Object call() throws Exception {
				System.out.println("executorService 是否为守护线程 :" + Thread.currentThread().isDaemon());
				return null;
			}
		});
		final CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
			System.out.println("this is lambda supplyAsync");
			System.out.println("supplyAsync 是否为守护线程 " + Thread.currentThread().isDaemon());
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("this lambda is executed by forkJoinPool");
			return "result1";
		});
		final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
			System.out.println("this is task with executor");
			System.out.println("supplyAsync 使用executorService 时是否为守护线程 : " + Thread.currentThread().isDaemon());
			return "result2";
		}, executorService);
		System.out.println(completableFuture.get());
		System.out.println(future.get());
		executorService.shutdown();

		System.out.println("==========================runAsync==========================");
		CompletableFuture<Void> future2 = CompletableFuture.runAsync(()->{
			System.out.println("qwer");
		},executorService);
	}
}
